package com.example.Lezedata.ui.activity.person;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.view.LayoutInflaterCompat;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.util.GBTimeManager;

import java.util.ArrayList;

import bid.Bid;
import common.Common;

class TransactionsVcCell extends ConstraintLayout{
    TextView time;
    TextView round;
    TextView money;

    private Bid.UserFinanceJournal journal;
    public static TransactionsVcCell newInstance(Context context) {
        TransactionsVcCell fragment = (TransactionsVcCell)LayoutInflater.from(context).inflate(R.layout.transactions_vc_cell,null,false);
        return fragment;
    }
    public TransactionsVcCell(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        time = findViewWithTag("time");
        round = findViewWithTag("round");
        money = findViewWithTag("money");
    }

    public void setJournal(Bid.UserFinanceJournal journal) {
        this.journal = journal;
        time.setText(String.format("Time: %s", GBTimeManager.timesYMD_HMS(journal.getCreateTime())));
//        round.setText(String.format("%s|%s",journal.get));
    }
}

public class TransactionsVc extends GBBaseVc {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_transactions_vc;
    }

    @Override
    public CharSequence title() {
        return "Transactions";
    }

    private int pageStart = 0;
    private int pageSize = 20;
    Bid.JournalType journalType = Bid.JournalType.JT_DEFAULT;

    ListView listView;
    BaseAdapter adapter;
    ArrayList<Bid.UserFinanceJournal> models = new ArrayList();
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        listView = findViewByTag("tableView");
        setAdapter();
        loadData();
    }

    void setAdapter(){
        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return models.size();
            }

            @Override
            public Bid.UserFinanceJournal getItem(int position) {
                return models.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null){
                    convertView = TransactionsVcCell.newInstance(mContext);
                }
                return convertView;
            }
        };
        listView.setAdapter(adapter);
    }

    void loadData(){
        Common.PageInfo pageInfo = Common.PageInfo.newBuilder().setPageSize(pageSize).setPageStart(pageStart).build();
        GBNetRequest.stubUser().userTransRecordList(Bid.UserTransRecordListRequest.newBuilder()
                .setJournalType(journalType)
                .setPageInfo(pageInfo)
                .build(), ServerResponseObserver.observer(new ServerResponseObserver.requestListener<Bid.UserTransRecordListResponse>() {
            @Override
            public void complete(Bid.UserTransRecordListResponse value, GBError error) {
                if (error != null){
                    if (pageStart == 0){
                        models.clear();
                    }
                    models.addAll(value.getListList());
                    adapter.notifyDataSetChanged();
                }
            }
        }));
    }
}